var OnEvent = Class.create();
OnEvent.prototype = {
	initialize : function(eventName, elementId, stop, requestUrl,
			onCompleteCallback) {
		if (!$(elementId))
			throw (elementId + " doesn't exist!");

		this.element = $(elementId);
		this.stop = stop;

		this.onCompleteCallback = null

		if (onCompleteCallback.length > 0)
			this.onCompleteCallback = onCompleteCallback;

		this.requestUrl = requestUrl;

		Event.observe(this.element, eventName, this.reflectOnEvent.bind(this),
				false);
	},
	reflectOnEvent : function(myEvent) {
		var fieldValue;
		var formElement = this.element.form;

		if (this.stop)
			Event.stop(myEvent);

		if (typeof formElement != 'undefined')
			fieldValue = $F(this.element);

		new Ajax.Request(this.reBuildURL(this.requestUrl, fieldValue),
				{
					method :'post',
					onFailure : function(t) {
						alert(t.responseText);
					},
					onException : function(t, exception) {
						alert(exception);
					},
					onSuccess : function(t) {
						if (this.onCompleteCallback != null)
							eval(this.onCompleteCallback + "('"
									+ t.responseText + "')");
					}.bind(this)
				});
	},
	reBuildURL : function(url, fieldValue) {
		var newUrl = "";
		var result = url.split(/[\?;&%]/);
		for ( var i = 0; i < result.length; i++) {
			if (i == 0) {
				newUrl = result[i];
				if (typeof fieldValue != 'undefined')
					newUrl += "/" + fieldValue;
			} else
				newUrl += "?" + result[i];
		}

		return newUrl;
	}
}

var ButtonEvent = Class.create();

ButtonEvent.prototype = {
	initialize : function(elementId, requestUrl) {
		if (!$(elementId))
			throw (elementId + " doesn't exist!");

		this.element = $(elementId);
		this.requestUrl = requestUrl;

		Event.observe(this.element, "click", this.fireEvent.bind(this,
				this.element), false);
	},
	fireEvent : function(myEvent) {
		document.location = this.requestUrl;
		return false;
	}
}